﻿using System.Collections.Generic;
using System.IO;

using FanaticToy.Core.IdentifierStrategy;
using FanaticToy.Core.Utils;

namespace FanaticToy.Core.Configure
{
    public class FileSystemLevel1FormatToyStrategy : BaseToyStrategy
    {
        ///<summary>第二项的起始裁切列表。</summary>
        public List<string> StartFlagList { get; set; } = new List<string>();

        ///<summary>第二项的结束裁切列表。</summary>
        public List<string> EndFlagList { get; set; } = new List<string>();

        public override bool IsMatched(string path)
        {
            var fileName = Path.GetFileNameWithoutExtension(path);

            var isMatched = PathUtils.IsFileNameMatched(fileName, StartFlagList, EndFlagList);

            return isMatched;
        }

        public override IEnumerable<string> GetAvaliablePaths(string basePath)
        {
            return Directory.GetFiles(basePath);
        }

        public override CodeMetaData GetCodeMetaData(string filePath, string fileId, string memberId)
        {
            var fileName = Path.GetFileNameWithoutExtension(filePath);
            var isMatched = PathUtils.IsFileNameWithIdMatched(fileName, fileId, StartFlagList, EndFlagList);

            if (isMatched == false) return null;

            var codeMetaData = new CodeMetaData();
            codeMetaData.FilePath = filePath;

            codeMetaData.LineId = GetMethodLine(filePath, memberId);

            return codeMetaData;
        }

        ///<summary>获得方法所在行数。</summary>
        private int GetMethodLine(string filePath, string methodName)
        {
            using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                using (var sr = new StreamReader(fs))
                {
                    var lineCounter = 0;
                    var maxLine = 500;
                    while (lineCounter < maxLine)
                    {
                        var line = sr.ReadLine();

                        if (MethodMetaUtils.TryGetMethodName(line, out var outputMethodName))
                        {
                            if (string.Equals(outputMethodName, methodName, System.StringComparison.OrdinalIgnoreCase))
                            {
                                return lineCounter;
                            }
                        }
                        // methodName (

                        lineCounter++;
                    }
                    return -1;
                }
            }


        }
        
        public override LocationIdentifier GetLocationIdentifier(string filePath, int lineId)
        {
            if (IsMatched(filePath) == false) return null;

            var fileName = Path.GetFileNameWithoutExtension(filePath);
            var fileId = PathUtils.GetTrimedResult(fileName, StartFlagList, EndFlagList);

            var methodId = MethodMetaUtils.GetMethodNameByLineId(filePath, lineId);

            return new LocationIdentifier() { FileId = fileId, MemberId = methodId };   

            //return base.GetLocationIdentifier(filePath, lineId);
        }

    }
}
